package com.icore.ui;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.icore.AppContext;
import com.icore.AppException;
import com.icore.R;
import com.icore.bean.RetRes;
import com.icore.common.UIHelper;
import com.icore.ui.base.BaseActivity;

/**
 * 购车新建
 * 
 */
public class GoucheNewActivity extends BaseActivity {

	private String userID = "";

	private ImageView editIV;
	private PopupWindow popupWindow;
	private LinearLayout layout;
	private ListView listView;
	private String title[] = { "保存" };

	// 基本信息
	private EditText binameET;
	private EditText bicompanyaddrET;
	private EditText bihomeaddrET;
	private EditText biphoneET;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gouche_new_view);

		// 获取传递过来的数据
		getIntentEntra();

		// 返回
		ImageView backIV = (ImageView) findViewById(R.id.backID);
		backIV.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});

		editIV = (ImageView) findViewById(R.id.editID);
		editIV.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int x = getWindowManager().getDefaultDisplay().getWidth() * 2 / 3;
				int y = editIV.getBottom() * 3 / 2;

				showPopupWindow(x, y);
			}

		});

		binameET = (EditText) findViewById(R.id.baseinfonameET);
		bicompanyaddrET = (EditText) findViewById(R.id.baseinfocompanyaddrET);
		bihomeaddrET = (EditText) findViewById(R.id.baseinfohomeaddrET);
		biphoneET = (EditText) findViewById(R.id.baseinfophoneET);
	}

	private void getIntentEntra() {
		Intent intent = getIntent();
		userID = intent.getStringExtra("user_id");
	}

	public void showPopupWindow(int x, int y) {
		layout = (LinearLayout) LayoutInflater.from(GoucheNewActivity.this).inflate(R.layout.pop_group, null);
		listView = (ListView) layout.findViewById(R.id.lv_dialog);
		listView.setAdapter(new ArrayAdapter<String>(GoucheNewActivity.this, R.layout.pop_item, R.id.tv_text, title));

		popupWindow = new PopupWindow(GoucheNewActivity.this);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 3);
		popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(layout);
		popupWindow.showAtLocation(findViewById(R.id.titleBarRL), Gravity.LEFT | Gravity.TOP, x, y + 5);// 需要指定Gravity，默认情况是center.

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				popupWindow.dismiss();
				popupWindow = null;

				insertBaseInfo();
			}
		});
	}

	private ProgressDialog mProgress;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (mProgress != null) {
				mProgress.dismiss();
			}
			if (msg.what == 1) {
				RetRes retRes = (RetRes) msg.obj;
				int code = retRes.getResCode();
				// 失败，弹出提示框
				if (code == 0) {
					UIHelper.ToastMessage(GoucheNewActivity.this, retRes.getResMessage());
				}
				// 成功,跳转
				else if (code == 1) {
					UIHelper.startActivity(GoucheNewActivity.this, GoucheListActivity.class, new BasicNameValuePair("user_id", userID));
				}
			} else {
				((AppException) msg.obj).makeToast(GoucheNewActivity.this);
			}
		}
	};

	private void insertBaseInfo() {

		mProgress = ProgressDialog.show(GoucheNewActivity.this, null, "加载中...", true, true);

		new Thread() {

			@Override
			public void run() {
				Message msg = new Message();
				try {
					Editable name = binameET.getText();
					if (name != null && name.length() > 0) {
						Map<String, Object> params = new HashMap<String, Object>();
						params.put("user_id", userID);
						params.put("name", URLEncoder.encode(binameET.getText().toString()));
						params.put("company", URLEncoder.encode(bicompanyaddrET.getText().toString()));
						params.put("home", URLEncoder.encode(bihomeaddrET.getText().toString()));
						params.put("phone", URLEncoder.encode(biphoneET.getText().toString()));

						RetRes retRes = AppContext.server.insertBI(params);
						msg.what = 1;
						msg.obj = retRes;
					} else {
						if (mProgress != null) {
							mProgress.dismiss();
						}
						return;
					}
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
}